package com.example.mercury.my.drinked;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import com.example.mercury.R;
import com.example.mercury.classify.DetailedActivity;
import com.example.mercury.entity.User;
import com.example.mercury.entity.WantOrDrinked;
import com.example.mercury.main.CategoryActivity;
import com.example.mercury.my.adapter.WantOrDrinkedAdapter;
import com.example.mercury.my.favorite.CoffeeFavoriteActivity;
import com.example.mercury.utils.HttpUtils;
import com.example.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/3 17:11
 * @projectName TurningCoffee
 * @className CoffeeDrinkedActivity.java
 * @description TODO
 */
public class CoffeeDrinkedActivity extends AppCompatActivity {
    private SharedPreferences userInfo;
    private User user;

    private AppCompatButton btn_find;
    private ListView lv_find;

    private WantOrDrinked drinked;

    private Activity activity;
    private HttpUtils httpUtils;
    private JsonUtils jsonUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_drinked);

        initViews();
        getData();
    }

    void getData(){
        Log.e("CoffeeDrinked", "getData: "+ user.getData().getUser().getUserId());
        Call call = httpUtils.getCall("/coffee/coffee/getTastedCoffee/" + user.getData().getUser().getUserId());
//        Call call = httpUtils.getCall("/coffee/coffee/getTastedCoffee/" + "10bc96d2-6003-430a-95f2-710209e67e0a");
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                Log.e("CoffeeDrinkedActivity", "onResponse: "+res);
                drinked = jsonUtils.getWantOrDrinked(res);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        WantOrDrinkedAdapter adapter = new WantOrDrinkedAdapter(activity, drinked.getData().getList(), mListener);
                        lv_find.setAdapter(adapter);
                    }
                });
            }
        });
    }

    void initViews(){
        activity = CoffeeDrinkedActivity.this;
        httpUtils = new HttpUtils();
        jsonUtils = new JsonUtils();

        userInfo = getSharedPreferences("user", 0);
        user = jsonUtils.getUser(userInfo.getString("userInfo", ""));
        lv_find = findViewById(R.id.lv_coffeedrinked);
    }

    private WantOrDrinkedAdapter.CardOnClickListerner mListener = new WantOrDrinkedAdapter.CardOnClickListerner() {
        @Override
        public void myOnClick(int position, View v) {
            Intent intent = new Intent(CoffeeDrinkedActivity.this, CoffeeFavoriteActivity.class);
            intent.putExtra("coffeeInfo", (Serializable)drinked.getData().getList().get(position));
            startActivity(intent);
        }
    };
}
